<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="handleCancel" center width="50%">
      <el-form ref="editFormRef" :model="editForm" :rules="rules" label-width="100px" size="small">
        <el-form-item label="岗位编号" prop="positionSn">
          <el-input placeholder="请输入岗位编号" v-model="editForm.positionSn"></el-input>
        </el-form-item>

        <el-form-item label="岗位名称" prop="positionName">
          <el-input placeholder="请输入岗位名称" v-model="editForm.positionName"></el-input>
        </el-form-item>

        <el-form-item label="岗位状态" prop="isEnable">
          <el-radio-group v-model="editForm.isEnable">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="岗位职责" prop="positionDuty">
          <el-input
            placeholder="请输入岗位职责"
            type="textarea"
            v-model="editForm.positionDuty"
            :maxlength="100"
            :autosize="{ minRows: 4, maxRows: 6 }"
          ></el-input>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input
            placeholder="请输入备注"
            type="textarea"
            v-model="editForm.remark"
            :maxlength="100"
            :autosize="{ minRows: 4, maxRows: 6 }"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel" size="small">取 消</el-button>
        <el-button type="primary" @click="handleOk('editFormRef')" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import pick from 'lodash.pick'
import { addPosition, modifyPosition } from '../js/auth'

export default {
  name: 'StationModal',
  data () {
    return {
      title: '添加岗位',
      dialogVisible: false,
      model: {},
      editForm: {
        positionSn: '',
        positionName: '',
        isEnable: '',
        positionDuty: '',
        remark: ''
      },
      rules: {
        positionSn: [{ required: true, message: '请输入岗位编号!' }],
        positionName: [{ required: true, message: '请输入岗位名称!' }],
        isEnable: [{ required: true, message: '请选择岗位状态!' }],
        positionDuty: [{ required: true, message: '请输入岗位职责!' }]
      }
    }
  },
  methods: {
    // 新增
    add () {
      this.edit({})
    },

    // 编辑
    edit (record) {
      this.dialogVisible = true
      this.model = Object.assign({}, record)
      if (record.positionId) {
        this.title = '编辑岗位'
        this.$nextTick(() => {
          this.editForm = pick(this.model, 'positionSn', 'positionName', 'isEnable', 'positionDuty', 'remark')
        })
      } else {
        this.title = '添加岗位'
      }
    },

    // 提交表单
    handleOk (editFormRef) {
      this.$refs[editFormRef].validate(async valid => {
        if (valid) {
          let res, msg
          if (!this.model.positionId) {
            res = await addPosition(this.editForm)
            msg = '保存成功！'
          } else {
            res = await modifyPosition(
              Object.assign({}, this.editForm, {
                positionId: this.model.positionId
              })
            )
            msg = '修改成功！'
          }
          if (res.code === 0) {
            this.$message.success(msg)
          } else {
            this.$message.warning(res.msg)
          }
          this.$emit('ok')
          this.handleCancel()
        }
      })
    },

    // 取消
    handleCancel () {
      this.dialogVisible = false
      this.$refs.editFormRef.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped></style>
